<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * conlabzCMS
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Article
 *
 * Allow the user to manage website articles
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Checkin24 extends User_Controller {
	function Checkin24( ) {
		parent::User_Controller( );

		$this->load->helper( 'form' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'checkin24' ), 'checkin24/user/checkin24' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Checkin24' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the checkin24 model
		$this->load->model( 'checkin24_model' );
		$this->load->module_model( 'article', 'website_model' );
		
		// Load the article language module
		$this->lang->module_load( 'checkin24', 'checkin24' );
		
		log_message( 'debug', 'conlabz CMS : Checkin24 class loaded' );
	}

	/**
	 * View Object categories
	 *
	 * @access public
	 */
	function index( ) {
		// Get Article Infomation
		$data['checkin24'] = $this->checkin24_model->getCheckin24( );

		// Display Page
		$data['header'] = $this->lang->line( 'checkin24_overview' );
		$data['page']   = $this->config->item('backendpro_template_user') . "/view_checkin24";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'checkin24';
		$this->load->module_view( 'article', $this->_container, $data );
	}


	/**
	 * View Objects
	 *
	 * @access public
	 */
	function objects( $checkin24_id ) {
		// Get Article Infomation
		$data['objects'] = $this->checkin24_model->getObjects( $checkin24_id );
		$data['checkin24_id'] = $checkin24_id;
		// Display Page
		$data['header'] = $this->lang->line( 'checkin24_overview' );
		$data['page']   = $this->config->item('backendpro_template_user') . "/view_objects";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'checkin24';
		$this->load->module_view( 'article', $this->_container, $data );
	}
	
	/**
	 * Set Article Defaults
	 *
	 * Specify what values should be shown in the fields when creating
	 * a new article by default
	 *
	 * @access private
	 * @todo specify the fields
	 */
	function _set_checkin24_defaults( ) {
		//$this->validation->set_default_value('field1','value');
		//$this->validation->set_default_value('field2','value');
	}

	/**
	 * Get Article Details
	 *
	 * Load article detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_checkin24_details( ) {
		$data['id']            = $this->input->post( 'id'            );
		$data['teaser']        = $this->input->post( 'teaser'        );
		$data['active']        = $this->input->post( 'active'        );
		$data['url_title']     = $this->input->post( 'url_title'     );
		$data['body_text']     = $this->input->post( 'body_text'     );
		$data['menu_title']    = $this->input->post( 'menu_title'    );	
		$data['category_id']   = $this->input->post( 'category_id'   );
		$data['article_title'] = $this->input->post( 'article_title' );

		return $data;
	}

	/**
	 * Display Article Form
	 *
	 * @access public
	 * @param integer $id Article ID
	 */
	function form( $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']            = "ID";
		$fields['teaser']        = $this->lang->line( 'article_teaser'        );
		$fields['active']        = $this->lang->line( 'article_active'        );
		$fields['category']      = $this->lang->line( 'article_category'      );
		$fields['body_text']     = $this->lang->line( 'article_body_text'     );
		$fields['article_title'] = $this->lang->line( 'article_article_title' );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create article rules (make sure no-one has the same short name)
			$rules['teaser']        = "trim";
			$rules['body_text']     = "trim|required";
			$rules['article_title'] = "trim|required";
		} else {
			// Use edit article rules (make sure no-one other than the current user has the same short name)
			$rules['teaser']        = "trim";
			$rules['body_text']     = "trim|required";
			$rules['article_title'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$article = $this->article_model->getArticles( 'article.id = ' . $id );
			$article = $article->row_array( );
			$this->validation->set_default_value( $article );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category_id', $this->preference->item( 'article_category' ) );
			$this->validation->set_default_value( 'active', '1' );
			// Setup article defaults
			$this->_set_article_defaults( );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Construct Category dropdown
			$website = " AND website_id = " . $this->session->userdata( 'website_id' );
			$this->load->model( 'category_model' );
			$data['category'] = $this->category_model->buildCategoryDropdown( 'category', 'id', $website );
			// Display form
			$this->validation->output_errors( );
			
			$data['fckeditor'] = array(
              	'name'        => 'body_text',
              	'id'          => 'body_text',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config' => array( 'CustomConfigurationsPath' => DEVPATH.'template/'.$this->session->userdata( 'template' ).'/js/fckconfig.js' )
			);
    		
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_article' ) : $this->lang->line( 'edit_article' ) );
			$this->bep_site->set_crumb( $data['header'],'article/user/article/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_article";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$article = $this->_get_article_details( );
				$article['timestamp'] = date( 'Y-m-d H:i:s' );

				$this->db->trans_start( );
				$this->article_model->insert( $article );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $article['article_title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_user') ) );
				}
				redirect( 'article/user/article' );
			} else {
				// SAVE
				$article = $this->_get_article_details( );
				$article['timestamp'] = date( 'Y-m-d H:i:s' );
				
				$this->db->trans_start( );
				$this->article_model->update( $article, $article['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'article_saved' ), $article['article_title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect('article/user/article');
			}
		}
	}

	/**
	 * Get Object Details
	 *
	 * Load object detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_object_details( ) {
		$data['id']            = $this->input->post( 'id' );
		$data['type']          = $this->input->post( 'type' );

		return $data;
	}
	
	/**
	 * Display Object Form
	 *
	 * @access public
	 * @param integer $id Article ID
	 */
	function form_objects( $checkin24_id, $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']        = $this->lang->line( 'object_id' );
		$fields['type']      = $this->lang->line( 'object_type' );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create article rules (make sure no-one has the same short name)
			$rules['id']     = "trim|required";
		} else {
			// Use edit article rules (make sure no-one other than the current user has the same short name)
			$rules['id']     = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$object = $this->checkin24_model->getObjects( $id );
			$this->validation->set_default_value( $object );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Display form
			$this->validation->output_errors( );
			$data['checkin24_id'] = $checkin24_id;
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_object' ) : $this->lang->line( 'edit_object' ) );
			$this->bep_site->set_crumb( $data['header'],'checkin24/user/checkin24/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_objects";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'checkin24';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$object = $this->_get_object_details( );

				$this->db->trans_start( );
				$this->checkin24_model->insertObject( $checkin24_id, $object );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $object['id'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_object') ) );
				}
				redirect( 'checkin24/user/checkin24/objects/'.$checkin24_id );
			} else {
				// SAVE
				$object = $this->_get_object_details( );
				
				$this->db->trans_start( );
				$this->checkin24_model->updateObject( $object, $object['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'object_saved' ), $object['id'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_object' ) ) );
				}
				redirect( 'checkin24/user/checkin24/objects/'.$checkin24_id );
			}
		}
	}
	
	/**
	 * Delete
	 *
	 * Delete the selected article from the system
	 *
	 * @access public
	 */
	function delete( ) {	
		/*if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'article/user/article', 'location' );
		}

		foreach( $selected as $article ) {
			$this->article_model->delete( $article );
		}

		flashMsg( 'success', $this->lang->line( 'article_deleted' ) );
		redirect( 'article/user/article', 'location' );*/
	}
	


	/**
	 * Delete
	 *
	 * Delete the selected object from the category
	 *
	 * @access public
	 */
	function delete_object( $checkin24_id ) {	
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'checkin24/user/checkin24/objects/'.$checkin24_id, 'location' );
		}

		foreach( $selected as $checkin24 ) {
			$this->checkin24_model->delete_object( $checkin24, $checkin24_id );
		}

		flashMsg( 'success', $this->lang->line( 'checkin24_deleted_object' ) );
		redirect( 'checkin24/user/checkin24/objects/'.$checkin24_id, 'location' );
	}
}
/* End of file article.php */
/* Location: ./modules/article/controllers/user/article.php */
